package com.poker.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;

/**
 * @Author: yuanZ;
 * @Date: 2018/9/25 16:54;
 * @Description: 时间处理类;
**/
public class DateTimeUtils {

    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter DATETIME_FORMATTER =  DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * @param: [];
     * @return: java.time.LocalTime;
     * @Description: 返回当前时间;
     */
    public static LocalTime getCurrentLocalTime() {
        return LocalTime.now();
    }

    /**
     * @param: [];
     * @return: java.time.LocalDate;
     * @Description: 返回当前日期;
     */
    public static LocalDate getCurrentLocalDate() {
        return LocalDate.now();
    }

    /**
     * @Author: yuanZ;
     * @Date: 2018/9/25 17:01;
     * @Description: 获取当前时间日期;
     **/
    public static LocalDateTime getCurrentLocalDateTime() {
        return LocalDateTime.now();
    }

    /**
     * @param: [time];
     * @return: java.time.LocalTime;
     * @Description: String ====> HH:mm:ss;
     */
    public static LocalTime parseLocalTime(String time) {
        return LocalTime.parse(time, TIME_FORMATTER);
    }

    /**
     * @param: [time, pattern];
     * @return: java.time.LocalTime;
     * @Description: String ====> 自定义时间格式;
     */
    public static LocalTime parseLocalTime(String time, String pattern) {
        return LocalTime.parse(time, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * @param: [date];
     * @return: java.time.LocalDate;
     * @Description: String ====> yyyy-MM-dd;
     */
    public static LocalDate parseLocalDate(String date) {
        return LocalDate.parse(date, DATE_FORMATTER);
    }

    /**
     * @param: [date, pattern];
     * @return: java.time.LocalDate;
     * @Description: String ====> 转自定义日期格式;
     */
    public static LocalDate parseLocalDate(String date, String pattern) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * @param: [dateTime];
     * @return: java.time.LocalDateTime;
     * @Description: String ====> yyyy-MM-dd HH:mm:ss;
     */
    public static LocalDateTime parseLocalDateTime(String datetime) {
        return LocalDateTime.parse(datetime, DATETIME_FORMATTER);
    }

    /**
     * @param: [date, pattern];
     * @return: java.time.LocalDate;
     * @Description: String ====> 转自定义日期时间格式;
     */
    public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) {
        return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * @param: [time];
     * @return: java.lang.String;
     * @Description: 时间转String[HH:mm:ss];
     */
    public static String formatLocalTime(LocalTime time) {
        return time.format(TIME_FORMATTER);
    }

    /**
     * @param: [time];
     * @return: java.lang.String;
     * @Description: 日期转String[yyyy-MM-dd];
     */
    public static String formatLocalDate(LocalDate date) {
        return date.format(DATE_FORMATTER);
    }

    /**
     * @param: [time];
     * @return: java.lang.String;
     * @Description: 时间转String[yyyy-MM-dd HH:mm:ss];
     */
    public static String formatLocalDateTime(LocalDateTime datetime) {
        return datetime.format(DATETIME_FORMATTER);
    }

    /**
     * @param: [time];
     * @return: java.lang.String;
     * @Description: 时间按照自定义格式转String[HH:mm:ss];
     */
    public static String formatLocalTime(LocalTime time, String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * @param: [time];
     * @return: java.lang.String;
     * @Description: 日期按照自定义格式转String[yyyy-MM-dd];
     */
    public static String formatLocalDate(LocalDate date, String pattern) {
        return date.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * @param: [time];
     * @return: java.lang.String;
     * @Description: 日期时间按照自定义格式转String[yyyy-MM-dd HH:mm:ss];
     */
    public static String formatLocalDateTime(LocalDateTime datetime, String pattern) {
        return datetime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * @param: [startDate, endDate];
     * @return: int;
     * @Description: 获取2个日期之间的间隔天数;
     */
    public static int periodDay(LocalDate startDate, LocalDate endDate) {
        return Period.between(startDate, endDate).getDays();
    }

    /**
     * @param: [startDate, endDate];
     * @return: long;
     * @Description: 获取2个日期之间的间隔时长;
     */
    public static long durationHour(Temporal startDate, Temporal  endDate) {
        return Duration.between(startDate, endDate).toHours();
    }

    /**
     * @param: [startDate, endDate];
     * @return: long;
     * @Description: 获取2个日期之间的间隔分钟;
     */
    public static long durationMinutes(Temporal startDate, Temporal  endDate) {
        return Duration.between(startDate, endDate).toMinutes();
    }

}
